package com.leyou.item.service;

import com.leyou.common.enums.ExceptionEnum;
import com.leyou.common.exceptions.LyException;
import com.leyou.common.utils.BeanHelper;
import com.leyou.item.dto.SpecGroupDTO;
import com.leyou.item.dto.SpecParamDTO;
import com.leyou.item.entity.SpecGroup;
import com.leyou.item.entity.SpecParam;
import com.leyou.item.mapper.SpecGroupMapper;
import com.leyou.item.mapper.SpecParamMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @package com.leyou.item.service
 * @description: 商品规格组service层
 * @author: 许超越
 * @date 2019/6/26 19:17
 * @version: V1.0
 */
@Service
public class SpecService {

    @Autowired
    private SpecGroupMapper specGroupMapper;

    @Autowired
    private SpecParamMapper specParamMapper;

    /**
     * 查询商品规格组
     * @param: cid 商品分组id
     * @return
     */
    public List<SpecGroupDTO> querySpecGroupByCid(Long cid) {
        SpecGroup specGroup = new SpecGroup();
        specGroup.setCid(cid);
        //查询规格分组
        List<SpecGroup> specGroupList = specGroupMapper.select(specGroup);
        //判断是否查询到
        if (CollectionUtils.isEmpty(specGroupList)) {
            throw new LyException(ExceptionEnum.BRAND_NOT_FOUND);
        }
        return BeanHelper.copyWithCollection(specGroupList, SpecGroupDTO.class);
    }


    /**
     * 根据规格组id查询规格参数
     * @param: gid 组id
     * @return
     */
    public List<SpecParamDTO> querySpecParams(Long gid, Long cid, Boolean searching) {
        //查询必须使用gid或者cid中的一个
        if (gid == null && cid == null && searching == null) {
            //抛出无效的参数异常
            throw new LyException(ExceptionEnum.INVALID_PARAM_ERROR);
        }
        //设置查询条件
        SpecParam specParam = new SpecParam();
        specParam.setGroupId(gid);
        specParam.setCid(cid);
        specParam.setSearching(searching);
        //查询规格参数
        List<SpecParam> specParamList = specParamMapper.select(specParam);
        //判断是否查询到数据
        if (CollectionUtils.isEmpty(specParamList)) {
            throw new LyException(ExceptionEnum.BRAND_NOT_FOUND);
        }
        return BeanHelper.copyWithCollection(specParamList, SpecParamDTO.class);
    }

    /**
     * 新增商品规格分组
     * @param: specGroup 商品规格分组的信息封装对象
     */
    public void insertSpecGroup(SpecGroup specGroup) {
        //新增分组
        int insertCount = specGroupMapper.insertSelective(specGroup);
        //判断新增是否成功
        if (insertCount != 1) {
            throw new LyException(ExceptionEnum.INSERT_OPERATION_FAIL);
        }
    }


    /**
     * 更新商品规格分组
     * @param: specGroup 品牌规格分组信息封装对象
     */
    public void updateSpecGroup(SpecGroup specGroup) {
        //更新分组信息
        int updateCount = specGroupMapper.updateByPrimaryKeySelective(specGroup);
        //判断是否修改成功
        if (updateCount != 1) {
            throw new LyException(ExceptionEnum.UPDATE_OPERATION_FAIL);
        }
    }


    /**
     * 根据规格分组id删除规格分组
     * @param: id 规格分组id
     */
    public void deleteSpecGroup(Long id) {
        //删除规格分组
        int delCount = specGroupMapper.deleteByPrimaryKey(id);
        //判断删除是否成功
        if (delCount != 1) {
            throw new LyException(ExceptionEnum.DELETE_OPERATION_FAIL);
        }
    }


    /**
     * 新增规格参数
     * @param: specParam 规格参数信息
     */
    public void insertSpecParam(SpecParam specParam) {
        //新增规格参数
        int insertCount = specParamMapper.insertSelective(specParam);
        //判断是否新增成功
        if (insertCount != 1) {
            throw new LyException(ExceptionEnum.INSERT_OPERATION_FAIL);
        }
    }


    /**
     * 修改规格参数
     * @param: specParam 规格参数信息
     */
    public void updateSpecParam(SpecParam specParam) {
        //修改规格参数
        int updateCount = specParamMapper.updateByPrimaryKeySelective(specParam);
        //判断修改是否成功
        if (updateCount != 1) {
            throw new LyException(ExceptionEnum.UPDATE_OPERATION_FAIL);
        }
    }


    /**
     * 根据规格参数id删除规格参数
     * @param: id 规格参数信息
     */
    public void deleteSpecParam(Long id) {
        //删除规格参数
        int delCount = specParamMapper.deleteByPrimaryKey(id);
        //判断删除是否成功
        if (delCount != 1) {
            throw new LyException(ExceptionEnum.DELETE_OPERATION_FAIL);
        }
    }


    /**
     * 查询规格参数组，及组内参数
     * @param id 商品分类id
     * @return 规格组及组内参数
     */
    public List<SpecGroupDTO> querySpecsByCid(Long id) {
        //根据分类id查询所有规格组
        List<SpecGroupDTO> groupList = querySpecGroupByCid(id);
        //根据所有分类查询参数
        List<SpecParamDTO> params = querySpecParams(null, id, null);
        //将规格参数按groupId分组
        Map<Long, List<SpecParamDTO>> paramMap = params.stream().collect(Collectors.groupingBy(SpecParamDTO::getGroupId));
        //将对应分组的规格参数方进对应组的对象中
        for (SpecGroupDTO groupDTO : groupList) {
            groupDTO.setParams(paramMap.get(groupDTO.getId()));
        }
        return groupList;
    }
}
